<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <ChartLine/>
            </n-icon>
            <Handle id="ArimaModel-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="ArimaModel-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>销售量预测模型</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="销售量预测模型">

                <p>销售变量：</p>
                <n-select
                    v-model:value="field"
                    :options="props.data.data.title"
                    @update:value="popup.reopen"
                    placeholder="请选择所要清洗的字段列"
                    label-field="title"
                    value-field="key"
                />
                <p>向后预测期数：</p>
                <n-select
                    v-model:value="forecast_steps"
                    :options="forecast_steps_array"
                    @update:value="popup.reopen"
                    placeholder="请选择所要清洗的字段列"
                />


                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="began">预测</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {ChartLine} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";
import {toChinesNumber} from "@/hooks/toChinesNumber";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()
// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            console.log(22)
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})
const field = ref('')
const forecast_steps_array = []
const createChineseNumberArray = (number) => {
    let chineseNumber = null
    for (let i = 1; i <= number; i++) {
        chineseNumber = toChinesNumber(i)
        if (chineseNumber[1] === "十" && chineseNumber[0] === "一") {
            chineseNumber = chineseNumber.slice(1)
        }
        forecast_steps_array.push(
            {label: chineseNumber + "期", value: i}
        );
    }
}
createChineseNumberArray(100000)

const forecast_steps = ref(1)
const began = () => {
    workApi.post("perform_tasks/arima", {
        data: props.data.data,
        column: field.value,
        forecast_steps: forecast_steps.value,
        units: "期"
    })
        .then(r => {
            props.data.data = r.data
            props.data.data.title.unshift(
                {key: "0", title: "数据名称", type: "str"}
            )
            props.data.data.data[0]["0"] = "预测值"
            props.data.data.data[1]["0"] = "真实值"
            props.data.data.data[2]["0"] = "拟合值"
            workApi.post("perform_tasks/row_column2",{data:props.data.data}).then(r=>props.data.data = r.data)
        })

}

const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.data = {...j.data.data};
                    props.data.data.title.map(s => {
                        // 移除可修改
                        delete s.render
                        return s
                    })
                    return
                }
            }
        }
    }
}
</script>

<style scoped lang="less">
.n-card {
    p {
        margin-top: 20px;
    }
}
</style>